const Generator = require('yeoman-generator');
const fs = require('fs');

const dirArr = [];
function mapDir(dir) {
    let res = [];
    files = fs.readFileSync(dir);{
        files.forEach(function (file) {
            const pathname = dir + '/' + file;
            const stat = fs.lstatSync(pathname);

            if (!stat.isDirectory()) {
                res.push(pathname.substring(pathname.indexOf('/') + 1));
            } else {
                res = res.concat(mapDir(pathname));
            }
        })
        return res;
    }
}

module.exports = class extends Generator {
    // 以交互的方式询问些问题
    prompting () {
        return this.prompt([
            {
                type: 'input',
                name: 'name',
                message: 'your project name',
                default: this.appname
            }
        ])
        .then(answers => {
            this.answers = answers
        })
    }
    writing () {
        // 把每一个文件都通过模版转换到目标路径
        const templates = mapDir(this.templatePath())
        templates.forEach(item => {
            this.fs.copyTpl(
                this.templatePath(item),
                this.destinationPath(item),
                this.answers
            )
        })
    }
}